<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">你的位置</div>
        <div class="button-list">
          <button>{{this.city}}</button>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <button v-for="item in hotCities" :key="item.id" @click="handleChangeClick(item.name)">{{ item.name }}</button>
        </div>
      </div>
      <div class="area" v-for="(item,key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{ key }}</div>
        <div class="item-list" v-for="innerItem in item" :key="innerItem.id">
          <div class="item" @click="handleChangeClick(innerItem.name)">{{ innerItem.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  data () {
    return {}
  },
  methods: {
    handleChangeClick (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  },
  computed: {
    ...mapState(['city'])
  }
}
</script>

<style scoped lang="stylus">
.list
  position absolute
  top 1.6rem
  left 0
  right 0
  bottom 0
  overflow hidden
  .title
    background #dedede
    font-size .28rem
    padding-left .28rem
    color #666
    height .6rem
    line-height .6rem
  .button-list
    padding .05rem .28rem
    width 100%
    display flex
    justify-content space-between
    flex-wrap wrap
    align-items center
    font-size .28rem
    button
      display block
      width 30%
      margin .12rem 0
      height .6rem
      line-height .6rem
      border-radius .08rem
      border .02rem solid #ccc
      background #fff
      text-align center
      font-size .28rem
  .item
    line-height .6rem
    font-size .28rem
    padding-left .28rem
    border-bottom .02rem solid #ccc
</style>
